package org.nightrunner.d3.model;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.nightrunner.d3.log.SysLog;

import com.google.gson.JsonArray;

/**
 * 分页的显示Bean、传送Bean
 * 
 * @author yibing888@gmail.com
 * 
 */
public class PaginationModel<T> {
	private static Logger logger = SysLog.BASE;
	/**
	 * 总页面数量
	 */
	private int pageAmount;
	/**
	 * 总记录数
	 */
	private int allRowSize;
	/**
	 * 当前页码
	 */
	private int currentPage = 0;
	/**
	 * 需要显示数据结构
	 */
	private T object;
	/**
	 * 页面上最多显示的行数
	 */
	private int rowSize = 10;

	/**
	 * 花费时间
	 */
	private float spentTime;

	private JsonArray paramArray;

	public JsonArray getParamArray() {
		return paramArray;
	}

	public void setParamArray(JsonArray paramArray) {
		this.paramArray = paramArray;
	}

	public float getSpentTime() {
		return spentTime;
	}

	public void setSpentTime(long spentTime) {
		this.spentTime = spentTime;
	}

	public int getPageAmount() {
		this.pageAmount = allRowSize % rowSize == 0 ? allRowSize / rowSize
				: allRowSize / rowSize + 1;
		return pageAmount;
	}

	public int getAllRowSize() {
		return allRowSize;
	}

	public void setAllRowSize(int allRowSize) {
		this.allRowSize = allRowSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public T getObject() {
		return object;
	}

	public void setObject(T object) {
		this.object = object;
	}

	public int getRowSize() {
		return rowSize;
	}

	public void setRowSize(int rowSize) {
		this.rowSize = rowSize;
	}

	@SuppressWarnings("unchecked")
	public void setHibernateQuery(Query query) {
		this.allRowSize = query.list().size();
		query.setFirstResult(this.currentPage * this.rowSize);
		query.setMaxResults(this.rowSize);
		this.object = (T) query.list();
		if (logger.isDebugEnabled()) {
			logger.debug(this.allRowSize);
			logger.debug(query.list().size());
			logger.debug((T) query.list());
			logger.debug(this.object);
		}
	}

	/**
	 * Set the pagination info
	 * 
	 * @param paginationInfo
	 *            The pagination info
	 */
	public void setPaginationInfo(IPaginationInfo paginationInfo) {
		setCurrentPage(paginationInfo.getCurrentPage());
		setRowSize(paginationInfo.getRowSize());
	}

	public void setPageAmount(int size) {
		this.pageAmount = size;
	}
}
